import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/zc'
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login/index.vue'),
    },
    {
      path: '/layout',
      name: 'layout',
      component: () => import('../views/layout/index.vue'),
    },
    {
      path: '/zc',
      name: 'zc',
      component: () => import('../views/zc/index.vue'),
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('../views/home/index.vue'),
    },
    {
      path: '/403',
      name: '403',
      component: () => import('../views/403/index.vue'),
    },
    {
      path: '/404',
      name: '404',
      component: () => import('../views/404/index.vue'),
    },
  ],
})

// GOOD
let lz = ["login", "zc", "404"]
router.beforeEach((to, from, next) => {
  let accessToken = localStorage.getItem('accessToken')
  if (!accessToken) {
    if (lz.includes(to.path)) {
      return next()
    }
    else {
      return next('/login')
    }
  }
  else {
    if (to.path=='/accessToken') {
      return next('/layout')
    }
    else {
      return next()
    }
  }
})
export default router
